package com.example.java.Controller;


import com.alibaba.fastjson.JSON;
import com.example.java.Entity.Admin;
import com.example.java.Mapper.LoginMapper;
import com.example.java.Service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController

//@CrossOrigin(origins = "*")
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public LoginService loginService;

    @Autowired
    public LoginMapper loginMapper;


    //    注册
    @PostMapping("/enroll")
    public String enroll(@RequestBody Admin admin){
        int enroll = loginService.addUser(admin);
        if (enroll > 0){
            return "注册成功";
        }
        return null;
    }

    //    登录
    @PostMapping("/login")
    public String login(@RequestBody Admin admin){
        log.info("login传入参数:{}", JSON.toJSONString(admin));
        int login = loginService.login(admin);
        if (login>0){
            return "登录成功";
        }
        return "登录失败";
    }

    //  忘记密码
    @PostMapping("/forget")
    public String forget(@RequestBody Admin admin){
        int result = loginService.updateUser(admin);
        if(result>0){
            return "重置成功";
        }
        return "重置失败";
    }
}
